import { DlJson } from '../DlJson';
import { ReaderIn } from '../ReaderIn';
import { TypeConstants } from '../TypeConstants';
import { WriterOut } from '../WriterOut';
import { ITypeAdapter } from './ITypeAdapter';

export class MapTypeAdapter implements ITypeAdapter {
  read(readerIn: ReaderIn): ReaderIn {
    return readerIn
  }

  write(writerOut: WriterOut): WriterOut {
    let target = writerOut.getTarget() as Map<string, ESObject>
    let targetValue = new DlJson()
    target.forEach((value, key,map1) => {
      let mapType = writerOut.getTypeFactory().create(value)
      let mapOut = new WriterOut(value, writerOut.getTypeFactory())
      targetValue[key] = mapType.write(mapOut).value()
    })
    writerOut.setWriteValue(targetValue)
    return writerOut
  }

  typeConstants(): TypeConstants {
    return TypeConstants.TYPE_MAP
  }
}